<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EventController
 *
 * @author tuancasi
 */
Yii::import('common.models.Event');

class EventController extends Controller {

    //put your code here
    public function actionIndex() {
	$model = new Event('search');
	$model->unsetAttributes();  // clear any default values
	if (isset($_GET['Event']))
	    $model->attributes = $_GET['Event'];
	$this->render('index', array('model' => $model));
    }

    /**
     * 
     */
    public function actionCreate() {
	$model = new Event();
	if (isset($_POST['submit'])) {
	    $model->attributes = $_POST['Event'];
	    if ($model->validate()) {
		$model->status = Event::STATUS_APPROVE;
		if ($model->save()) {
		    $this->redirect(BACKEND_SITE_URL . 'event');
		}
	    }
	}
	$this->render('create', array('model' => $model));
    }

    /**
     * 
     */
    public function actionUpdate($id) {
	$model = $this->_loadModel($id);

	if (isset($_POST['submit'])) {

	    $model->attributes = $_POST['Event'];
	    $model->title = $_POST['Event']['title'];
	    $model->description = $_POST['Event']['description'];
	    if ($model->validate()) {
//		$model->status = Event::STATUS_APPROVE;
		if ($model->save()) {
		    Yii::app()->user->setFlash('success', '<div class="alert alert-success">You have updated successfully</div>');
		}
	    }
	}
	$this->render('update', array('model' => $model));
    }

    public function actionDelete($id) {
	$model = $this->_loadModel($id);
	$model->delete();
    }

    /**
     * 
     * @param type $id
     * @return type
     * @throws CHttpException
     */
    public function _loadModel($id) {
	$model = Event::model()->findByPk($id);
	if ($model) {
	    return $model;
	} else {
	    throw new CHttpException(404, 'The page is not existed');
	}
    }

}
